#include "http.hpp"

#define BASE_DIR "./wwwroot/"

string echo(const http_request &req) {
    stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for(auto &p : req._params) {
        ss << p.first << ": " << p.second;
    }
    for(auto &headers : req._headers) {
        ss << headers.first << ": " << headers.second << "\r\n";
    }
    ss << "\r\n" << req._context;
    return ss.str();
}

void get(const http_request &req, http_response *res) {
    res->set_context(echo(req), "text/plain");
}

void post(const http_request &req, http_response *res) {
    res->set_context(echo(req), "text/plain");
}

void put(const http_request &req, http_response *res) {
    Ultil::write_file(BASE_DIR + req._path, req._context);
}

void del(const http_request &req, http_response *res) {
    res->set_context(echo(req), "text/plain");
}

int main() {
    http_server s;
    s.set_thread_count(3);
    s.set_base_static_dir(BASE_DIR);
    s.get("/hello", ::get);
    s.post("/login", ::post);
    s.put("/hello.txt", ::put);
    s.del("/del.txt", ::del);

    s.start();
    return 0;
}